package com.e3.sysmg.security.client;

import com.e3.sysmg.security.model.RoleModel;
import com.e3.web.core.client.EditorListener;
import com.e3.web.core.client.GXTHelper;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.client.ListenerManager;
import com.e3.web.widget.client.MyDialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;
/**
 * Role编辑器由三个部分组成。
 * Role自身属性（form）。
 * 继承的角色（List，左右选择）。
 * 拥有的模块（列表，显示所有模块包括继承角色拥有的模块）。
 * @author Janty
 *
 */
public class RoleEditorDialog extends MyDialog {
	
	private static RoleMessages MESSAGES = RoleMessages.INSTANCE;
	
	private RoleRPCServiceAsync service = RoleRPCService.Util.getInstance();
	
	private RoleFormPanel roleFormPanel;
	
	private boolean isNew = true;
	
	private ListenerManager<RoleModel> listenerManager = new ListenerManager<RoleModel>();
	
	public RoleEditorDialog(){
	}
	
	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		this.setSize(750, 700);
		setLayout(new FitLayout());
		add(getRoleFormPanel());
		
		setOKText(MESSAGES.action_save());
	}
	
	public RoleFormPanel getRoleFormPanel(){
		if(roleFormPanel == null){
			roleFormPanel = new RoleFormPanel();
		}
		return roleFormPanel;
	}
	
	public void addEditorListeners(EditorListener<RoleModel> listener){
		listenerManager.addEditorListener(listener);
	}
	
	public void showDialog(RoleModel roleModel,boolean isNew){
		this.isNew = isNew;
		if(isNew){
			this.setHeading(MESSAGES.title_new(MESSAGES.local_name()));
		}
		else{
			this.setHeading(MESSAGES.title_modify(roleModel.getName()));
		}
		getRoleFormPanel().setFormValue(roleModel);
		this.setVisible(true);
	}

	@Override
	protected void onOk() {
		RoleModel role = getRoleFormPanel().getFormValue();
		if(role == null){
			return;
		}
		
		String code = role.getCode();
		String name = role.getName();
		if(code==null||code.trim().length()==0){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_notnull(RoleHelper.INSTANCE.getFieldDisplayName(RoleModel.CODE)), null);
			return;
		}
		if(name==null||name.trim().length()==0){
			MessageBox.alert(MESSAGES.msgbox_alert_title(),MESSAGES.msg_notnull(RoleHelper.INSTANCE.getFieldDisplayName(RoleModel.NAME)), null);
			return;
		}
		GXTHelper.maskSave(this);
		if(isNew){
			newRole(role);
		}
		else{
			modifyRole(role);
		}
	}
	
	private void modifyRole(final RoleModel model) {
		service.modifyRole(model, new GwtAsyncCallback<RoleModel>(this) {
			@Override
			public void doSuccess(RoleModel result) {
				setVisible(false);
				getRoleFormPanel().updateFromListData(result);
				listenerManager.notifyEditorSuccess(result);
			}
		});
	}

	private void newRole(final RoleModel roleModel) {
		service.newRole(roleModel, new GwtAsyncCallback<RoleModel>(this) {
			@Override
			public void doSuccess(RoleModel result) {
				setVisible(false);
				getRoleFormPanel().addRoleToFromList(result);
				listenerManager.notifyEditorSuccess(result);
			}
		});
	}
}
